$(function() {
    var grid = $("#gridOfficeCustomer").datagrid({
        title: "客户信息",
        iconCls: "icon-save",
        methord: "post",
        url: "/Main/GetOfficeCustomer/",
        sortName: "ID",
        sortOrder: "desc",
        idField: "ID",
        pageSize: 20,
        striped: true, //奇偶行是否区分
        columns: [[
                    { field: "TheName", title: "企业名称"},
					{ field: "LegalPerson", title: "法人"},
					{ field: "LPPhone", title: "法人联系电话"},
					{ field: "RegisteredCapital", title: "注册资金"},
					{ field: "Profit", title: "企业利润"},
					{ field: "AnnualIncome", title: "年收入额"},
					{ field: "FoundDate", title: "成立日期", formatter: $.getJsonDate},
                    { field: "Phone", title: "联系电话"},
                    { field: "Contact", title: "联系人"},
					{ field: "Addr", title: "地址"}
				]],
        fit: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: false,
		onDblClickRow: function (rowIndex, rowData) {  //双击事件
            $("<div></div>").dialog({
				title: "客户业务信息",
				modal: true,
				resizable: true,
				width: 900,
				height: 300,
				href: "/Main/CustomerBusiness",
				onClose:function(){  
                    $(this).dialog("destroy");
				},
				onLoad: function(){
					CustomerList.GetBusiness(rowData.ID);
				},
				onLoadError: function(data, status, error){
					if(data.status == "400")
						indexPage.reLogin();
				}
			});
        },
		onLoadError: function(data, status, error){
			if(data.status == "400")
				indexPage.reLogin();
			else
				$.messager.alert("错误", data.responseText, "error");
		}
    });
    $("body").layout();
});


var CustomerList = {}

CustomerList.GetBusiness = function(id){
	    var grid = $("#gridCustomerBusiness").datagrid({
        iconCls: "icon-save",
        methord: "post",
        url: "/Main/GetCustomerBusiness",
		queryParams: {
		customerid: id},
		sortName: "FillInDate",
        sortOrder: "desc",
        idField: "ID",
        pageSize: 20,
        striped: true, //奇偶行是否区分
        columns: [[
		            { field: "TheName", title: "客户名称"},
					{ field: "BusinessCode", title: "编号" },                    
                    { field: "TypeName", title: "项目名称"},
					{ field: "Charges", title: "合同金额"},
					{ field: "BeginDate", title: "合同开始日期", formatter: $.getJsonDate},
					{ field: "EndDate", title: "合同终止日期", formatter: $.getJsonDate},
					{ field: "Introducer", title: "客户介绍人"},
					{ field: "Agent", title: "项目经办人"},
					{ field: "OfficeName", title:"所属部室"},
					{ field: "DepartmentName", title: "所属分局"},
                    { field: "OwingMoney", title: "欠费", formatter: function (value, row, index) {
                        if(row.Verifier!=null && row.Verifier != "")
						{
						    if (value>=0) {
                                return "否";
					        }
							else
								return value;
						}
						else
							return "";
					    }
					},
					{ field: "BillDate", title: "开票日期", formatter: $.getJsonDate},
					{ field: "BillUnitName", title: "开票单位"},
					{ field: "FillIn", title:"填表人"},
					{ field: "Verifier", title:"审核"},
					{ field: "Paidup", title:"免除余款", formatter: function (value, row, index) {
                        if(row.Verifier!=null && row.Verifier != "")
						{
						    if (value && row.OwingMoney<0) {
                                return "是";
					        }
							else
								return "否";
						}
						else
							return "";
					    }}
				]],

        rownumbers: true,
        fitColumns: true,
		onLoadError: function(data, status, error){
			if(data.status == "400")
				indexPage.reLogin();
		}
    });
}

CustomerList.DataGridSearch = function(){
	var customer = $("#customerListName").val();
	$("#gridOfficeCustomer").datagrid({ url: "/Main/GetOfficeCustomer/", queryParams: { customer: customer} });
}